<?php

namespace App\Console\Commands;

use App\Library\Logic\ShopProductLogic;
use Exception;
use Illuminate\Console\Command;

class ProductReducedCommand extends Command
{
    /**
     * 捡漏商品每小时降价
     *
     * @var string
     */
    protected $signature = 'command:productReduced';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Execute the console command
     * @return void
     * @throws Exception
     */
    public function handle(): void
    {
        $list = ShopProductLogic::query()->with('ext')->where(['plug2' => 1, 'product_status' => 2])->get([
            'product_id', 'serial_number', 'min_price', 'max_price', 'product_special_start_time',
        ]);
        foreach ($list as $v) {
            if (now()->lt(now()->timestamp($v->product_special_start_time))) continue;
            $hour = now()->diffInHours(now()->timestamp($v->product_special_start_time));
            $price = $v->max_price - bcdiv($hour, $v->ext->plug_hour) * $v->ext->plug_num;
            if ($price <= 0) continue;
            else if ($price < $v->min_price) mq('plug', $v);
            $v->min_price = $price;
            $v->save();
        }
    }
}
